package io.renren.modules.factory.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.annotation.NoRepeatSubmit;
import io.renren.common.exception.RRException;
import io.renren.modules.factory.entity.TFactoryRawMeterialEntity;
import io.renren.modules.factory.service.TFactoryRawMeterialService;
import io.renren.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.factory.entity.TFactoryRawMeterialTypeEntity;
import io.renren.modules.factory.service.TFactoryRawMeterialTypeService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author steakliu
 * @email 2319492349@qq.com
 * @date 2021-03-19 23:32:58
 */
@RestController
@RequestMapping("factory/tfactoryrawmeterialtype")
public class TFactoryRawMeterialTypeController extends AbstractController {
    @Autowired
    private TFactoryRawMeterialTypeService tFactoryRawMeterialTypeService;
    @Autowired
    private TFactoryRawMeterialService factoryRawMeterialService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("factory:tfactoryrawmeterialtype:list")
    public R list(@RequestParam Map<String, Object> params){
        params.put("sellerId",getSeller());
        PageUtils page = tFactoryRawMeterialTypeService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("factory:tfactoryrawmeterialtype:info")
    public R info(@PathVariable("id") Long id){
		TFactoryRawMeterialTypeEntity tFactoryRawMeterialType = tFactoryRawMeterialTypeService.getById(id);

        return R.ok().put("tFactoryRawMeterialType", tFactoryRawMeterialType);
    }

    /**
     * 保存
     */
    @NoRepeatSubmit
    @RequestMapping("/save")
    @RequiresPermissions("factory:tfactoryrawmeterialtype:save")
    public R save(@RequestBody TFactoryRawMeterialTypeEntity tFactoryRawMeterialType){
        tFactoryRawMeterialType.setSellerId(getSeller());
        tFactoryRawMeterialType.setCreateTime(new Date());
		tFactoryRawMeterialTypeService.save(tFactoryRawMeterialType);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("factory:tfactoryrawmeterialtype:update")
    public R update(@RequestBody TFactoryRawMeterialTypeEntity tFactoryRawMeterialType){
		tFactoryRawMeterialTypeService.updateById(tFactoryRawMeterialType);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("factory:tfactoryrawmeterialtype:delete")
    public R delete(@RequestBody Long[] ids){
        TFactoryRawMeterialEntity one = factoryRawMeterialService.getOne(new QueryWrapper<TFactoryRawMeterialEntity>()
                .eq("seller_id", getSeller())
                .eq("raw_material_type_id", ids[0])
        );
        if (one != null){
            throw new RRException("当前原料类型下存在原料，无法删除");
        }
        tFactoryRawMeterialTypeService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
